package com.intel.two.controller.older;


import com.intel.two.common.R;
import com.intel.two.common.older.LayuiPageVo;
import com.intel.two.pojo.older.People;
import com.intel.two.pojo.older.Suite;
import com.intel.two.service.older.PeopleService;
import com.intel.two.service.older.SuiteService;
import com.intel.two.utils.OssUtil;
import com.intel.two.vo.older.BarVo;
import com.intel.two.vo.older.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/people")
public class PeopleController {

    @Autowired
    private PeopleService peopleService;

    @RequestMapping("/peoplelist")
    public LayuiPageVo<People> peoplePageData(@RequestParam Map<String, Object> params) {
        // 注意 这里使用 map来接受参数  前端传的不定参数 都可以封装到 map 中

        return peopleService.peoplePageData(params);
    }

    @RequestMapping("add")
    public R addStudent(People people) {


        int result = peopleService.addPeople(people);
        if (result == 1) {
            return R.ok();
        } else
            return R.error();
    }

    @RequestMapping("/delete")
    public R deleteById(Integer oldman_id) {
        System.out.println(oldman_id);
        int result = peopleService.deleteById(oldman_id);
        if (result == 1) {
            return R.ok();
        } else
            return R.error();
    }

    @GetMapping("/deleteAll")
    public R deleteAll(@RequestParam Map<String, Object> map) {

        int result = peopleService.deleteAll(map);
//        return result > 0 ? R.ok() : R.error();
        if (result > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/update")
    public R updatePeople(People people) {
        int result = peopleService.udatePeople(people);
        System.out.println(result);
        return result == 1 ? R.ok() : R.error();
    }

    @RequestMapping("/echartBar")
    public R echartBar() {

        List<BarVo> list = peopleService.sexBarData();
        System.out.println(list);
        return R.ok().data("sexBar", list);

    }

    @RequestMapping("/echartPie")
    public R echartPie() {
        List<PieVo> list = peopleService.sexPieData();
        return R.ok().data("sexPie", list);
    }

    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws Exception {
        //得到文件的名字
        String originalFilename = file.getOriginalFilename();

        String fileUrl = OssUtil.updateLoad(file.getInputStream(), originalFilename);
        // 上传成功之后的文件地址 写给前端

        return R.ok().data("url", fileUrl);
    }
}